<?php

/**
 * 自动生成 MerUserController
 * 
 * Apps/Backend/Controller/MerUserController.class.php
 * Zix @ 2015-06-15
 */

namespace Backend\Controller;

use Common\Service\MerchantService  ;
use Common\Service\MerUserService ;
use Common\Service\SysDictionaryService;

class MerUserController extends BackendController {

  public function index( $mer_id = 0 ) {
    $action = I('get.action', 'read');

    $data = $this->_init_data();
    $data['catalog_name'] = '商户';
    $data['catalog_url'] = U( $this->module . '/merchant/index') ;
    
    $data['module_name'] = '用户';
    $data['module_url'] = U($this->base_path . 'index') . '/' . $mer_id ;

    $Merchant = MerchantService::instance() ;
    $data['merchant_data'] = $Merchant->get_by_id($mer_id);
    if ( empty( $data['merchant_data'] ) ) {
      gameover('商户不存在');
    }
    
    $MerUser = MerUserService::instance();
    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id' => 'Merchant_Menu',
      'mer_id' => $mer_id ,
      'module_url' => U( $this->base_path . 'index' ),
      'read_url'   => U( $this->base_path . 'read'  ),
      'create_url' => U( $this->base_path . 'create'),
      'update_url' => U( $this->base_path . 'update'),
      'delete_url' => U( $this->base_path . 'delete'),
      'page' => 1,
      'page_size' => C('PAGE_SIZE'),
      'row' => $MerUser->get_default_row() 
    );

    $data['area'] = array(
      'get_default_url'     => U( $this->module . '/area/get_default'),
      'get_by_pid_url'      => U( $this->module . '/area/get_by_pid'),
      'get_by_district_url' => U( $this->module . '/area/get_by_district'),
    );
    
    $SysDictionary = SysDictionaryService::instance();
    $data['param'] = array(
      'status' => $SysDictionary->get_mer_user_status(),
    );

    $this->display_with_layout($data, $this->controller . ':index');
  }

  function read() {
    $post = I('post.');

    $config = array(
      'page' => $post['page'],
      'page_size' => $post['page_size'],
      'mer_id' => $post['mer_id'],
      'sort' => empty($post['sort']) ? 'id' : $post['sort'],
      'order' => empty($post['order']) ? 'DESC' : $post['order'],
    );

    $MerUser = MerUserService::instance();
    $data['rows'] = $MerUser->get_by_cond($config);

    $config['count'] = TRUE;
    $data['total'] = $MerUser->get_by_cond($config);

    $this->ajaxReturn($data);
  }

  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
    
    $post['password'] = trim($post['password']);
    if ( empty( $post['password'] ) ) {
      $this->ajaxReturn(ajax_arr('请填写密码'));
    }
    
    if ( empty( $post['binding_date'] ) ) {
      unset($post['binding_date']);
    }
    
    $post['password'] = md5($post['password']);
    
    $MerUser = MerUserService::instance();
    $ret = $MerUser->create($post);
    $this->ajaxReturn($ret);
  }

  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    $post['password'] = trim($post['password']);
    if ( empty( $post['password'] ) ) {
      unset($post['password']);
    } else {
      $post['password'] = md5($post['password']);
    }
    
    if ( empty( $post['binding_date'] ) ) {
      unset($post['binding_date']);
    }
    
    $MerUser = MerUserService::instance();
    $ret = $MerUser->update($id, $post);
    $this->ajaxReturn($ret);
  }

  function delete() {
    $ids = I('post.ids');
    $MerUser = MerUserService::instance();
    $ret = $MerUser->delete($ids); 
    $this->ajaxReturn($ret);
  }
}
